class LeaderController {
    def authenticateService
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [leaderInstanceList: Leader.list(params), leaderInstanceTotal: Leader.count()]
    }

    def create = {
        def leaderInstance = new Leader()
        leaderInstance.properties = params
        return [leaderInstance: leaderInstance]
    }

	private saveRole(def person) {  
		def config = authenticateService.securityConfig
		def leaderRole = config.security.leaderRole
		def role = Role.findByAuthority(leaderRole)
		role.addToPeople(person) 
	}

    def save = {
        def leaderInstance = new Leader(params)
        leaderInstance.passwd = authenticateService.encodePassword(params.passwd)
        if (leaderInstance.save(flush: true)) {
            saveRole(leaderInstance)
            flash.message = "创建成功"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [leaderInstance: leaderInstance])
        }
    }

    def show = {
        def leaderInstance = Leader.get(params.id)
        if (!leaderInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leader.label', default: 'Leader'), params.id])}"
            redirect(action: "list")
        }
        else {
            [leaderInstance: leaderInstance]
        }
    }

    def edit = {
        def leaderInstance = Leader.get(params.id)
        if (!leaderInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leader.label', default: 'Leader'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [leaderInstance: leaderInstance]
        }
    }

    def update = {
        def leaderInstance = Leader.get(params.id)
        if (leaderInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (leaderInstance.version > version) {
                    
                    leaderInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'leader.label', default: 'Leader')] as Object[], "Another user has updated this Leader while you were editing")
                    render(view: "edit", model: [leaderInstance: leaderInstance])
                    return
                }
            }
            leaderInstance.properties = params
            if (!leaderInstance.hasErrors() && leaderInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'leader.label', default: 'Leader'), leaderInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [leaderInstance: leaderInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leader.label', default: 'Leader'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def leaderInstance = Leader.get(params.id)
        if (leaderInstance) {
            try {
                leaderInstance.delete(flush: true)
                flash.message = "删除成功"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'leader.label', default: 'Leader'), leaderInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'leader.label', default: 'Leader'), params.id])}"
            redirect(action: "list")
        }
    }
}
